package com.zzw.entity;



import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Table(name = "sys_user")
public class SysUser extends AbstractAuditingEntity implements UserDetails {// implements UserDetails, CredentialsContainer {
    /**
     * 名称，唯一性
     */
    private String name;

    /**
     * 密码
     */
    private String psw;

    /**
     * 创建者ID
     */
    @Column(name = "create_id")
    private String createId;

    /**
     * 电话号码
     */
    private String phone;



    @Transient
    private List<SysRole> sysRoles;


    /**
     * 获取名称，唯一性
     *
     * @return name - 名称，唯一性
     */
    public String getName() {
        return name;
    }

    /**
     * 设置名称，唯一性
     *
     * @param name 名称，唯一性
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取密码
     *
     * @return psw - 密码
     */
    public String getPsw() {
        return psw;
    }

    /**
     * 设置密码
     *
     * @param psw 密码
     */
    public void setPsw(String psw) {
        this.psw = psw;
    }

    /**
     * 获取创建者ID
     *
     * @return create_id - 创建者ID
     */
    public String getCreateId() {
        return createId;
    }

    /**
     * 设置创建者ID
     *
     * @param createId 创建者ID
     */
    public void setCreateId(String createId) {
        this.createId = createId;
    }

    /**
     * 获取电话号码
     *
     * @return phone - 电话号码
     */
    public String getPhone() {
        return phone;
    }

    /**
     * 设置电话号码
     *
     * @param phone 电话号码
     */
    public void setPhone(String phone) {
        this.phone = phone;
    }


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Set<GrantedAuthority> userAuthotities = new HashSet<>();
        for(SysRole role : this.sysRoles){
            List<SysFunPermission> sysFunPermissions = role.getSysFunPermissions();
            for(SysFunPermission authority : sysFunPermissions){
                userAuthotities.add(new SimpleGrantedAuthority(authority.getCode()));
            }
        }
        return userAuthotities;
    }

    @Override
    public String getPassword() {
        return this.getPsw();
    }

    @Override
    public String getUsername() {
        return this.getName();
    }

    @Override
    public boolean isAccountNonExpired() {
        return false;
    }

    @Override
    public boolean isAccountNonLocked() {
        return false;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return false;
    }

    @Override
    public boolean isEnabled() {
        return this.isEnabled();
    }
}